package com.xsq.audio.player.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;

import com.xsq.audio.player.PlayManager;

/**
 * Created by Shiquan.Xiao on 2023/4/3.
 */
public class SPUtils {

    public static final String SP_RECENT_AUDIO_ID = "sp_recent_audio_id";
    public static final String SP_RECENT_AUDIO_TITLE = "sp_recent_audio_title";
    public static final String SP_RECENT_AUDIO_POSITION = "sp_recent_audio_position";
    public static final String SP_AUDIO_PLAY_REPEAT_MODE = "sp_audio_play_repeat_mode";

    private static SharedPreferences preferences;

    public static SharedPreferences getPreferences() {
        if (preferences == null) {
            Context context = PlayManager.getContext();
            if (context == null) return null;

            preferences = context.getSharedPreferences("rs_audio_sp", Context.MODE_PRIVATE);
        }
        return preferences;
    }

    public static int getPlayMode() {
        return getInt(SP_AUDIO_PLAY_REPEAT_MODE, 0);
    }

    public static void savePlayMode(int mode) {
        putInt(SP_AUDIO_PLAY_REPEAT_MODE, mode);
    }

    public static boolean getBoolean(String key, boolean defValue) {
        if (getPreferences() == null) return false;
        return getPreferences().getBoolean(key, defValue);
    }

    public static void putBoolean(String key, boolean value) {
        if (getPreferences() == null) return;
        getPreferences().edit().putBoolean(key, value).apply();
    }

    public static float getFloat(String key, float defValue) {
        if (getPreferences() == null) return 0;
        return getPreferences().getFloat(key, defValue);
    }

    public static void putFloat(String key, float value) {
        if (getPreferences() == null) return;
        getPreferences().edit().putFloat(key, value).apply();
    }

    public static int getInt(String key, int defValue) {
        if (getPreferences() == null) return 0;
        return getPreferences().getInt(key, defValue);
    }

    public static void putInt(String key, int value) {
        if (getPreferences() == null) return;
        getPreferences().edit().putInt(key, value).apply();
    }

    public static long getLong(String key, long defValue) {
        if (getPreferences() == null) return 0;
        return getPreferences().getLong(key, defValue);
    }

    public static void putLong(String key, long value) {
        if (getPreferences() == null) return;
        getPreferences().edit().putLong(key, value).apply();
    }

    public static void putString(String key, @Nullable String value) {
        if (getPreferences() == null) return;
        getPreferences().edit().putString(key, value).apply();
    }

    public static String getString(String key, String defValue) {
        if (getPreferences() == null) return "";
        return getPreferences().getString(key, defValue);
    }

}
